package edu.hawaii.duedates;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.util.Calendar;
import java.util.Timer;
import org.junit.Test;


/**
 * Tests the DueDateTimer class which creates
 * an object for running a scheduled task of DueDates.
 * 
 * @author tylerwolff and Scheller Sanchez.
 *
 */
public class TestDueDateTimer {
  
  // The user's UH ID number and last name with 2 books checked out
  private final String uhTestId2 = System.getenv("DUEDATES_TESTUSER2_ID");
  private final String lastTestName2 = System.getenv("DUEDATES_TESTUSER2_LASTNAME");

  /**
   * Tests if DueDatesTimer runs if wakeup is greater than zero.
   */
  @Test
  public void testDueDateTimer () {
    String [] params = {"-uhm", "15516494", "sanchez", "-console", "-wakeup", "1"};
    
    DueDates dueDate = new DueDates(params);
    dueDate.setItemsDue(dueDate.getItems());
    if (dueDate.getWakeupValue() > 0) {
      Timer timer = new Timer();
      Calendar date = Calendar.getInstance();
      DueDateTimer dueDateTimer = new DueDateTimer(dueDate);
      timer.scheduleAtFixedRate(dueDateTimer, date.getTime(), (long) 
          ((dueDate.getWakeupValue()) * (24 * 60 * 60)));
      assertNotNull("Tests for successful -wakeup", dueDateTimer);
    }
    else {
      fail();
    }
    
  }
  
  /**
   * Tests if DueDatesTimer runs if wakeup is zero or less.
   * 
   */
  @Test
  public void testDueDateTimerZero () {
    String [] params = new String[6];
    params[0] = "-uhm";
    params[1] = uhTestId2;
    params[2] = lastTestName2;
    params[3] = "-console";
    params[4] = "-wakeup";
    params[5] = "0";
    
    DueDates dueDate = new DueDates(params);
    dueDate.setItemsDue(dueDate.getItems());
    if (dueDate.getWakeupValue() > 0) {
      fail("wakeup value has to be greater than zero");
    }
    else {
      assertEquals(0.0, dueDate.getWakeupValue(), 0);
    }
    
  }
  
}
